package com.gc.server.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import com.gc.server.vo.ShopVo;
import com.gc.server.vo.UserVo;
import com.qiniu.util.Auth;
import com.yf.request.SearchParam;
import com.yf.tz.entity.AccountEntity;
import com.yf.tz.service.AccountService;
import com.yf.utils.GenUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import com.gc.server.entity.UserEntity;
import com.gc.server.service.UserService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 用户Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Mar 10 11:17:45 CST 2020
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 列表用户
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:list")
    public R list(@RequestBody SearchParam<UserVo> params) {
        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<UserVo> userList = userService.queryListVo(params.getEntity());
        return R.ok(new PageInfo(userList));
    }

    /**
     * 列表用户
     */
    @RequestMapping("/vip")
    @RequiresPermissions("user:list")
    public R vip(@RequestBody SearchParam<UserVo> params) {
//        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<UserVo> userList = userService.queryListVo(params.getEntity());
        userService.changeVip(userList);
        return R.ok();
    }

    @Autowired
    private AccountService accountService;

    /**
     * 查看用户
     */
    @SysLog("查看用户")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.get(id);
        //获取用户的所有账户
        List<AccountEntity> accounts = accountService.findByUserId(id);
        return R.ok(user).putExtra("accounts",accounts);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("user:save")
    public R save(UserEntity user) {
        userService.save(user);

        return R.ok(user);
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("user:update")
    public R update(@RequestBody UserVo user) {
        userService.updateVo(user);
        return R.ok(user);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("user:delete")
    public R delete(Long[] ids) {
        userService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有用户
     */
    @SysLog("所有用户")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<UserEntity> userList = userService.queryList(params);

        return R.ok(userList);
    }

    /**
     * 账户转账
     */
    @SysLog("账户转账")
    @RequestMapping("/account")
    @RequiresPermissions("user:account")
    public R transfer(String fromUserName, String fromUserCode, String toUserName, String toUserCode, BigDecimal amount, Integer type, String info) {
        accountService.adminTransfer(fromUserName,fromUserCode,toUserName,toUserCode,amount,type,info);
        return R.ok();
    }

    /**
     * 下载订单
     */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("user:list")
    public void down(@RequestBody SearchParam<UserVo> params, HttpServletResponse response) {
        List<UserVo> list = userService.queryListVo(params.getEntity());
        userService.downLoad(response, list);
    }

    /**
    @ModelAttribute
    public SearchParam<UserEntity> searchParam(SearchParam<UserEntity> params, UserEntity user) {
        if (user != null) {
            params.setEntity(user);
        }

        return params;
    }
 **/
    /***********自定义开始***********/

    public static void main(String[] args) throws IOException, SQLException {
        GenUtils.init("jdbc:mysql://localhost:3306/gc", "root", "19941220", "/Users/huangfei/Desktop/code.zip", "com.yf.tz").generatorCode("shop");
    }
}
